package com.gitee.dupe.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.dolt.boot.service.ServiceCacheManager;
import com.gitee.dolt.boot.service.ServiceCacheRegister;
import com.gitee.dolt.boot.service.SuperCacheServiceImpl;
import com.gitee.dupe.authority.dao.ResourceMapper;
import com.gitee.dupe.authority.entity.Resource;
import com.gitee.dupe.authority.enumeration.ResourceTypeEnum;
import com.gitee.dupe.authority.extend.AuthorityCustomOperate;
import com.gitee.dupe.authority.service.IResourceService;
import com.gitee.dupe.common.cache.CacheKeyDefinition;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LYH
 * @since 2020-06-30
 */
@Service
@RequiredArgsConstructor
public class ResourceServiceImpl extends SuperCacheServiceImpl<ResourceMapper, Resource> implements IResourceService {

    private final AuthorityCustomOperate authorityCustomOperate;

    @Override
    protected String prefix() {
        return CacheKeyDefinition.RESOURCE;
    }

    @Override
    protected void register(ServiceCacheRegister register) {
        super.register(register);
//        register.register("OO", new ServiceCacheManager<>(getCacheKeyBuilder("OO"), Resource::getIdent));
    }

    @Override
    public List<Resource> findAllEnabledByUserId(Long userId) {
        return baseMapper.findAllEnabledByUserId(userId);
    }

    @Override
    public List<Resource> findAllByRoleId(Long roleId) {
        return baseMapper.findAllByRoleId(roleId);
    }

    @Override
    public List<Resource> findAllByType(ResourceTypeEnum type) {
        return listByCache().stream().filter(e -> type.equals(e.getType())).collect(Collectors.toList());
    }

    @Override
    public <E extends IPage<Resource>> E page(E page, Wrapper<Resource> queryWrapper) {
        if (authorityCustomOperate.resourceQueryWithParent()) {
            return (E) baseMapper.selectPageWithParent(page, queryWrapper);
        }
        return super.page(page, queryWrapper);
    }


}
